/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * AltaUsuario.java
 *
 * Created on 30-ago-2011, 11:19:39
 */
package Ventanas;

import DataTypes.DataUsuario;
import Objetos.PropiedadesIP;
import Proxies.CtrlUsuariosWS;
import Proxies.CtrlUsuariosWSService;
import Proxies.EnumVisibilidad;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Calendar;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.xml.namespace.QName;

/**
 *
 * @author Sebas
 */
public class AltaUsuario extends javax.swing.JFrame {

    /** Creates new form AltaUsuario */
    public AltaUsuario() {
        initComponents();
        Calendar c = Calendar.getInstance();
        Date d = c.getTime();
        jCalendarButton1.setTargetDate(d);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jButton1 = new javax.swing.JButton();
        vNombre = new javax.swing.JTextField();
        vNick = new javax.swing.JTextField();
        vMail = new javax.swing.JTextField();
        vImagen = new javax.swing.JTextField();
        vPais = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        vSexo = new javax.swing.JComboBox();
        vVisibilidad = new javax.swing.JComboBox();
        jLabel10 = new javax.swing.JLabel();
        vPass = new javax.swing.JPasswordField();
        vCiudad = new javax.swing.JTextField();
        jLabel11 = new javax.swing.JLabel();
        jCheckBox_notificaciones = new javax.swing.JCheckBox();
        jCalendarButton1 = new net.sourceforge.jcalendarbutton.JCalendarButton();

        jButton1.setText("Dar de alta");
        jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton1MouseClicked(evt);
            }
        });

        jLabel1.setText("Nombre");

        jLabel2.setText("Nick");

        jLabel3.setText("E-Mail");

        jLabel4.setText("Contraseña");

        jLabel5.setText("Imagen");

        jLabel6.setText("Fecha de nacimiento");

        jLabel7.setText("Sexo");

        jLabel8.setText("Pais");

        jLabel9.setText("Nivel de Visibilidad");

        vSexo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "M", "F" }));
        vSexo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                vSexoActionPerformed(evt);
            }
        });

        vVisibilidad.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Publico", "Parcial", "Privado" }));

        jLabel10.setText("RED - Alta Usuario");

        jLabel11.setText("Ciudad");

        jCheckBox_notificaciones.setText("Enviar notificaciones al usuario");

        jCalendarButton1.setText("Seleccionar fecha");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(54, 54, 54)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(7, 7, 7)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel8)
                            .addComponent(jLabel11)
                            .addComponent(jLabel6)
                            .addComponent(jLabel5)
                            .addComponent(jLabel4)
                            .addComponent(jLabel7)
                            .addComponent(jLabel3)
                            .addComponent(jLabel2)
                            .addComponent(jLabel1))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(vPass, javax.swing.GroupLayout.DEFAULT_SIZE, 317, Short.MAX_VALUE)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(vMail)
                                    .addComponent(vNick)
                                    .addComponent(vImagen, javax.swing.GroupLayout.DEFAULT_SIZE, 317, Short.MAX_VALUE)
                                    .addComponent(vNombre))
                                .addComponent(vPais, javax.swing.GroupLayout.DEFAULT_SIZE, 317, Short.MAX_VALUE)
                                .addComponent(vCiudad, javax.swing.GroupLayout.DEFAULT_SIZE, 317, Short.MAX_VALUE))
                            .addComponent(jCheckBox_notificaciones)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(vSexo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 284, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(jCalendarButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 117, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel10)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 344, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(27, 27, 27)
                        .addComponent(jLabel9)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(vVisibilidad, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 122, Short.MAX_VALUE)
                        .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 132, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(62, 62, 62))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel10)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(vNombre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(vNick, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(vMail, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addGap(12, 12, 12)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(vPass, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(vImagen, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5))
                .addGap(15, 15, 15)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(jCalendarButton1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(vSexo, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel7))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(vPais, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(vCiudad, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel11)))
                    .addComponent(jLabel8))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jCheckBox_notificaciones)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(vVisibilidad, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel9))
                        .addContainerGap(39, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap())))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void vSexoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_vSexoActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_vSexoActionPerformed

private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton1MouseClicked
// TODO add your handling code here:
    
    PropiedadesIP props = PropiedadesIP.getInstance();
    String IP = props.getServidorCentralIP();

    URL dirUsr = null;
    try {
        dirUsr = new URL(IP + "CtrlUsuarios?wsdl");
    } catch (MalformedURLException ex) {
        Logger.getLogger(AltaUsuario.class.getName()).log(Level.SEVERE, null, ex);
    }
    QName qNaUsr = new QName("http://WebServices/", "CtrlUsuariosWSService");
    CtrlUsuariosWS usu = new CtrlUsuariosWSService(dirUsr, qNaUsr).getCtrlUsuariosWSPort();
    String nivel = vVisibilidad.getSelectedItem().toString();
    EnumVisibilidad enumNiv = null;
    
    Date date = jCalendarButton1.getTargetDate();
    Calendar cal = Calendar.getInstance();
    cal.setTime(date);
    
    if ("Publico".equals(nivel))
        enumNiv = EnumVisibilidad.PUBLICO;
    if ("Privado".equals(nivel))
        enumNiv = EnumVisibilidad.PRIVADO;
    if ("Parcial".equals(nivel))
        enumNiv = EnumVisibilidad.PARCIAL;
    
    boolean nombre = true,nick = true,mail = true,pass = true,fechNac = true,pais = true,ciudad = true;
        if(vNombre.getText().isEmpty())
             nombre = false;
        if(vNick.getText().isEmpty())
             nick = false;
        if(vMail.getText().isEmpty())
             mail = false;
        if(vPass.getPassword().toString().isEmpty())
            pass = false;
        if(vPais.getText().isEmpty())
            pais = false;
        if(vCiudad.getText().isEmpty())
            ciudad = false;
        
        if(nombre==false || nick==false || mail==false || pass==false || fechNac==false || ciudad==false || pais==false)
        {
            if (nombre == false) 
                vNombre.requestFocusInWindow();
            else if (nick == false)
                vNick.requestFocusInWindow();
            else if (mail == false) 
                vMail.requestFocusInWindow();
            else if(pass == false)
                vMail.requestFocusInWindow();
           else  if (pais == false) 
                 vPais.requestFocusInWindow();
           else  if (ciudad == false) 
                vCiudad.requestFocusInWindow();
            
        JOptionPane.showMessageDialog(null, "Todos los campos son obligatorios");
    } else {
        DataUsuario Du = new DataUsuario(vMail.getText(), vNombre.getText(), vNick.getText(),
                String.valueOf(vPass.getPassword()),
                cal,
                vSexo.getSelectedItem().toString(),
                vCiudad.getText(),
                vPais.getText(),
                false,
                enumNiv,
                vImagen.getText(), 
                jCheckBox_notificaciones.isSelected(),
                false);
        
            estactrabajo.Transformador trans = new estactrabajo.Transformador();
            Proxies.DataUsuario chUsu = trans.changeUsuario(Du);
            usu.altaUsuario(chUsu);
            
            JOptionPane.showMessageDialog(null, "Se dio de alta correctamente");
            
     
    }
        
        
}//GEN-LAST:event_jButton1MouseClicked

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new AltaUsuario().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private net.sourceforge.jcalendarbutton.JCalendarButton jCalendarButton1;
    private javax.swing.JCheckBox jCheckBox_notificaciones;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JTextField vCiudad;
    private javax.swing.JTextField vImagen;
    private javax.swing.JTextField vMail;
    private javax.swing.JTextField vNick;
    private javax.swing.JTextField vNombre;
    private javax.swing.JTextField vPais;
    private javax.swing.JPasswordField vPass;
    private javax.swing.JComboBox vSexo;
    private javax.swing.JComboBox vVisibilidad;
    // End of variables declaration//GEN-END:variables
}
